package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.CalendarEventTypeDAO;
import org.jetnuke.entities.CalendarEventType;

public class GenericCalendarEventTypeDAO extends AutoKeys implements
		CalendarEventTypeDAO {

	public CalendarEventType selectByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CalendarEventTypeModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		CalendarEventType cet = new CalendarEventType();
		
		if (rs.next()) {
			fillCalendarEventTypeFromResultSet(cet, rs);
		}
		
		rs.close();
		p.close();

		return cet;
	}

	protected void fillCalendarEventTypeFromResultSet(CalendarEventType cet, ResultSet rs)throws Exception {
		cet.setID(rs.getInt("ID"));
		cet.setDESC(rs.getString("EVTYPE"));
		
	}

	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CalendarEventTypeModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			CalendarEventType u = new CalendarEventType();
			fillCalendarEventTypeFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

}
